﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;
using GroupChat2013.Entity;
using GroupChat2013.Common;

namespace GroupChat2013.Logic
{
    /// <summary>
    /// 解析应用程序配置文件
    /// </summary>
    public class ParseConfig<T> where T : class,new()
    {
        private string configPath;

        private string configFile;

        private string configName;

        private T configCache;

        private System.Func<T> defaultValue;

        /// <summary>
        /// 构造函数
        /// </summary>
        public ParseConfig(string configPath, string configName, System.Func<T> defaultValue)
        {
            this.configPath = configPath;
            this.configFile = Path.Combine(configPath, configName);
            this.defaultValue = defaultValue;
            this.configName = configName;

            InitConfig();
        }

        /// <summary>
        /// 解析
        /// </summary>
        public T Parse()
        {
            if (configCache != null)
                return configCache;

            string config = string.Empty;
            FileStream fs = File.Open(configFile, FileMode.Open);
            using (StreamReader sr = new StreamReader(fs))
            {
                config = sr.ReadToEnd();
            }
            fs.Close();

            T configInfo = JsonConvert.Parse<T>(config);
            if (configInfo == null)
                configInfo = defaultValue();

            configCache = configInfo;
            return configCache;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="basePara">需要保存的信息</param>
        public bool SaveConfig(T basePara)
        {
            try
            {
                if (basePara == null)
                    return false;

                string defaultConfig = JsonConvert.ConvertToJson(basePara);

                FileStream fs = File.Open(configFile, FileMode.Create, FileAccess.Write);
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(defaultConfig);
                }
                fs.Close();

                configCache = basePara;
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public bool SaveConfig()
        {
            if (configCache != null)
            {
                return SaveConfig(configCache);
            }

            return false;
        }

        /// <summary>
        /// 初始化配置信息
        /// </summary>
        private void InitConfig()
        {
            if (!File.Exists(configFile))
            {
                Tools.CreateDir(configPath);

                T config = defaultValue();
                string defaultConfig = JsonConvert.ConvertToJson(config);

                FileStream fs = File.Open(configFile, FileMode.OpenOrCreate);
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(defaultConfig);
                }
                fs.Close();
            }
        }
    }
}
